import Vue from 'vue'
import Vuex from 'vuex'
import util from '../utils/utils.js';

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		token: 'token',
		deviceUUID: '', //设备的UUID
		isNet: true,
		isConnected: false,
		isEiDevice: true,
		isBuildModel: null,
		isBluetoothAdapter: true,
		originalData: [], //原始数据
		userInfo: null, //用户信息 多个人的信息
		userBaseInfo: null, // 用户基础信息
		isDFU: false, //设备是否处于 DFU 模式 
		bleId: '', // 蓝牙设备的mac / id 地址
		isActiveDisConnect: false, //是否主动断开蓝牙设备
		isLoopCheckConnectState: true,
		regDataCenterApi: '/reg-data-center-service/public',


		isLoopCheckAdornState: true,  //是否循环检测头环佩戴情况
		firmwareVersion: '1.0.0',   //蓝牙设备的固件版本
		isReceipt: false,
		receiptTimer: null,

	// 	固件升级弹框
		dfuDialogFlag:false,
	// 	dfu文件包总数
		total: 0,
		count: 0,

		sendType: 0,
		percentage: 0,
		// 获取dfu信息给固件升级
		dfuDevices: [],
	},
	mutations: {
		
		token(state, stauts) {
			try {
				state.token = stauts;
			} catch (e) {
				//TODO handle the exception
				console.error(e);
			}
		},
		// UUID
		deviceUUID(state, stauts) {
			state.deviceUUID = stauts;
		},
		// 监听网络情况
		changeNetStatus(state, stauts) {
			state.isNet = stauts;
		},
		connectState(state, connectState) {
			state.isConnected = connectState;
			console.log('store值：' + connectState);
		},
		userInfo(state, userInfo) {
			try {
				state.userInfo = userInfo;
				uni.setStorageSync('userInfo', userInfo);
			} catch (e) {
				console.log(e);
			}
		},
		userBaseInfo(state, value) {
			state.userBaseInfo = value;
		},
		isBuildModel(state, stauts) {
			state.isBuildModel = stauts;
		},
		isActiveDisConnect(state, stauts) {
			state.isActiveDisConnect = stauts;
		},
		isLoopCheckConnectState(state, stauts) {
			state.isLoopCheckConnectState = stauts;
		},
		updateCurrentUser(state, currentUser) {
			state.userInfo.currentUser = currentUser;
		},
		UpdateAppSecret(state, AppSecret) {
			state.userInfo.currentUser.appSecret = AppSecret;
			state.userInfo.loginUserList.forEach(item => {
				if (item.userId == state.userInfo.currentUser.userId) item.appSecret = AppSecret;
			})
			try {
				uni.setStorageSync('userInfo', state.userInfo);
			} catch (e) {
				console.log(e);
			}
		},
		BluetoothAdapterState(state, isAdapter) {
			state.isBluetoothAdapter = isAdapter;
		},
		isEiDevice(state, iseiDevice) {
			state.isEiDevice = iseiDevice;
		},
		originalData(state, originalData) {
			let array = Array.prototype.slice.call(new Uint8Array(originalData));
			state.originalData = array;
		},
		isDFU(state, stauts) {
			state.isDFU = stauts;
		},
		BleId(state, stauts) {
			state.bleId = status;
		},
		SET_regApi(state, value) {
			state.regDataCenterApi = value;
		},

		isLoopCheckAdornState(state, val) {
			state.isLoopCheckAdornState = val;
		},

		firmwareVersion(state, val) {
			state.firmwareVersion = val;
		},

		isReceipt(state, val) {
			state.isReceipt = val;
		},
		receiptTimer(state, stauts) {
			state.receiptTimer = stauts;
		},

		setDfuDialogFlag(state, val){
			state.dfuDialogFlag = val;
		},

		setTotal(state, total) {
			state.total = total
		},
		setCount(state, count) {
			state.count = count
		},

		setSendType(state, type) {
			state.sendType = type
		},

		setPercentage(state, percentage) {
			state.percentage = percentage
		},
		setDfuDevices(state, dfuDevices){
			state.dfuDevices = dfuDevices
		}
	},
	actions: {
		updateOriginalData(context, originalData) {
			context.commit('originalData', originalData)
		}
	}
})

export default store